package cn.com.dhcc.ddi.node.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.vo.NodeInfo;
import cn.com.dhcc.ddi.node.vo.wapper.NodeInfoRowWapper;

/**
 * 
    * @ClassName: NodeInfoDao
    * @Description: 节点DAO
    * @author 党向阳
    * @date 2016-11-22
    *
 */
@Repository
public class NodeInfoDao extends BaseDao {

	private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
	
	/**
	 * 节点信息列表
	 * 
	 * @return
	 * @throws ServiceException
	 */
	
	public PagerData<NodeInfo> list(Pager pager, WhereCondition condition)
			throws DaoException {
				String msg = "";
				String sql = "select t1.*, t2.ad_nm orgName FROM "+ SI_TABLE("t_ddi_node_info") + " t1"+						   
								    " LEFT JOIN " + SI_TABLE("t_ddi_node_region") + " t2 on t1.orgcode = t2.ad_cd";								    					
				try {
					return CM.getDao().getPagerList("获取节点信息列表", sql,
							new NodeInfoRowWapper(), pager, condition, new Object[] {});
				} catch (DaoException e) {
					msg = "获取节点信息列表失败";
					logger.error("获取节点信息列表失败", e);
					throw new DaoException(msg, e);
				}
			}
	
	
	
	
	/**
     * 获取对象
     * @param id
     * @return
     * @throws DaoException
     */
    public NodeInfo get(String id) throws DaoException {
        return getDao().getModuleById("获取对象",NodeInfo.class,new Object[]{id});
    }
	
	 /**
     * 新增
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insert(NodeInfo obj) throws DaoException {
        return getDao().insertModule("新增",obj);
    }
	
    /**
     * 修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int update(NodeInfo obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改",obj);
    }
    
    
    /**
     * 新增或修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insertOrUpdate(NodeInfo obj) throws DaoException {
        return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改",obj);
    }
    
    /**
     * 验证名称唯一性
        * @Title: count
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @param @param searchParameter
        * @param @return
        * @param @throws DaoException    参数
        * @return int    返回类型
        * @throws
     */
    public int count(WhereCondition searchParameter) throws DaoException {
		String msg = "";
		String sql = "select count(*) from " + SI_TABLE("t_ddi_node_info");
		try {
			return CM.getDao().queryForInt("节点信息已存在", sql, searchParameter, new Object[]{});
		} catch (DaoException e) {
			msg = "获取节点信息列表失败";
			logger.error("获取节点信息列表失败", e);
			throw new DaoException(msg, e);
		}
		
	}
    
	/**
	 * 批量删除(备用)
	    * @Title: batchDelete
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param ids
	    * @param @return
	    * @param @throws DaoException
	    * @param @throws IllegalArgumentException    参数
	    * @return int    返回类型
	    * @throws
	 */
	
	public int batchDelete(String[] ids) throws DaoException,IllegalArgumentException {
               int num = 0;
				  try {
					 Assert.noNullElements(ids);
					 List<Object[]> params = new ArrayList<Object[]>();
					 for (String id : ids) {
						params.add(new Object[] { id });
					}
					 String sql = "delete from " + SI_TABLE("t_ddi_node_info") 
								+ " WHERE id=?";
					num = CM.getDao().batchDelete("批量删除", sql, params);
				} catch (DaoException e) {
					throw new DaoException("批量删除失败", e);
				}
				return num;
				}
	
	/**
	 * 
	    * @Title: getNodeInfo
	    * @Description: 获取节点
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return List<NodeInfo>    返回类型
	    * @throws
	 */
	public List<NodeInfo> getNodeInfo() throws DaoException {
		String msg = "";
		String sql = "select t1.*,t2.slszy_node slszyNode, t2.ad_nm orgName,t2.ad_cd nodeCode,t2.ad_nm nodeName FROM "
				   + SI_TABLE("t_ddi_node_info") + " t1"+						   
			    " LEFT JOIN " + SI_TABLE("t_ddi_node_region") + " t2 on t1.orgcode = t2.ad_cd"
			    +" ORDER BY t1.orgcode ASC ";	
				  
		try {
			return CM.getDao().list("获取节点列表", sql,
					new NodeInfoRowWapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取列表失败";
			logger.error("获取列表失败", e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 
	    * @Title: getTypeNodeInfo
	    * @Description: 获取省级行政区编码
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return List<NodeInfo>    返回类型
	    * @throws
	 */
	public List<NodeInfo> getTypeNodeInfo() throws DaoException {
		String msg = "";
		String sql = "SELECT DISTINCT  CONCAT(SUBSTRING(orgcode,1,2),'0000') orgcode, t2.ad_nm orgName FROM "+ SI_TABLE("t_ddi_node_info")+" , "+	SI_TABLE("t_ddi_node_region")+" t2 "
				+" where CONCAT(SUBSTRING(orgcode,1,2),'0000')=t2.ad_cd"
			    +" ORDER BY CONCAT(SUBSTRING(orgcode,1,2),'0000') ASC";	
				  
		try {
			return CM.getDao().list("获取省级行政区列表", sql,
					new NodeInfoRowWapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取列表失败";
			logger.error("获取列表失败", e);
			throw new DaoException(msg, e);
		}
	}
   
}
